package com.my.service.dataClean.impl;
import com.my.Repository.UserRepository;
import com.my.common.PageList;
import com.my.config.BaseDao;
import com.my.entity.User;
import com.my.service.dataClean.IDataUserService;
import com.my.util.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Map;

/**
 * Created by wt on 2017/10/16.
 */
@Service
//@Transactional(transactionManager = "tmpTransactionManager", readOnly = false, propagation = Propagation.REQUIRED)
public class DataUserServiceImpl implements IDataUserService {
    private Logger logger = LoggerFactory.getLogger(DataUserServiceImpl.class);

    @Autowired
    private BaseDao hxjbAppBaseDao;
    @Autowired
    private UserRepository userRepository;
    
    @Override
    public void insertUserBaseMsg(Map<String, Object> readParams) {

        Date startTime = readParams.get("startTime") == null ? null : (Date) readParams.get("startTime");
        Date endTime = readParams.get("endTime") == null ? null : (Date) readParams.get("endTime");

        logger.info("用户信息相关[{}]-[{}]开始",  DateUtil.Date2Str(startTime), DateUtil.Date2Str(endTime));

        int pageSize = 10000;
        int topage = 1;

        Boolean flag = true;
        while (flag) {
            PageList<User> pageList = this.hxjbAppBaseDao.queryForPageList("DataUserReadMapper.selectUserBaseMsg", readParams, topage, pageSize);
            if (pageList == null ||pageList.getPageSize() <=0 || pageList.getRecords() == null || pageList.getRecords().size() <= 0) {
                logger.info("用户信息相关[{}]-[{}],暂无记录",  DateUtil.Date2Str(startTime), DateUtil.Date2Str(endTime));
                break;

            }
            flag = pageList.hashNextPage();
            logger.info("用户信息相关[{}]-[{}],{}/{},总记录数:[{}]", DateUtil.Date2Str(startTime), DateUtil.Date2Str(endTime), pageList.getCurrentPageNo(),pageList.getTotalPage(), pageList.getTotalCount());

//            this.tmpBaseDao.insert("DataUserWriteMapper.insertUserBaseMsg", pageList.getRecords());
            this.userRepository.insert(pageList.getRecords());

            topage++;
        }

        logger.info("用户信息相关[{}]-[{}]结束",  DateUtil.Date2Str(startTime), DateUtil.Date2Str(endTime));


    }
}
